//
//  GetRegisterCodeViewController.m
//  Finance
//
//  Created by Hu Jianguo on 13-7-21.
//  Copyright (c) 2013年 cheng xiang. All rights reserved.
//

#import "GetRegisterCodeViewController.h"
#import "MultiThreading.h"
#import "AnalysisJson.h"
#import "AppDelegate.h"
@interface GetRegisterCodeViewController ()

@end

@implementation GetRegisterCodeViewController
@synthesize phoneNumber;
@synthesize btnGetPassword;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.phoneNumber.textColor = [UIColor grayColor];
    self.phoneNumber.text = @"请输入手机号码";
    self.phoneNumber.tag = 1;
    self.phoneNumber.delegate = self;
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    [leftItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [leftItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
    [self.navigationItem setLeftBarButtonItem:leftItem];
    UIColor* bk = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"beij.png"]];
    
    [self.view setBackgroundColor:bk];
    
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma - 获取验证码
-(IBAction)getUserRegisterCode:(id)sender
{
    if (self.phoneNumber.text == nil)
    {
        [self showMessageBox:@"提示" content:@"验证码错误."];
        return;
    }
    
    if ( [self.phoneNumber.text compare:@""] == 0)
    {
        [self showMessageBox:@"提示" content:@"验证码不能为空."];
        return;
    }
    
    if([self isPureInt:self.phoneNumber.text] == false)
    {
        [self showMessageBox:@"提示" content:@"手机号码不能为其他字符."];
        return;
    }
    
    NSString* strphonenumber = self.phoneNumber.text;
    //组请求报文
    NSDictionary *header = [AnalysisJson PackageJsonHeader:@"W104"];
    NSDictionary *body = [self setPackketBody:strphonenumber];
    
    NSData *requestJson = [AnalysisJson PackageJsonFromHeader:header andBody:body];
    NSString *jsonString = [[NSString alloc] initWithData:requestJson encoding:NSUTF8StringEncoding];
    
    NSLog(@"%@",jsonString);
    
    //发送报文
    [self getReturnRegisterCode: requestJson];
    [self.phoneNumber resignFirstResponder];
}

-(NSDictionary*)setPackketBody:(NSString*)number
{
    NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
    //dict[@"mobile"] = phoneNumber;
    [dict setObject:number forKey:@"mobile"];
    return dict;
}

-(void)getReturnRegisterCode:(NSData*)jsonData
{
    [MultiThreading getDataFromNetwork:[SERVICE_URL stringByAppendingString:@"SendSmsValidCode"] toObject:self callBack:@selector(requestRegisterCodeCallBack:) withBody:jsonData];
}

- (void)requestRegisterCodeCallBack:(NSDictionary *)dic
{
    //    NSError *error = nil;
    //    NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length] encoding:NSUTF8StringEncoding];
    //
    //    NSData *jsonData = [results dataUsingEncoding:NSUTF8StringEncoding];
    //    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    
    NSDictionary* body = dic[@"body"];
    if(body != nil && body.count != 0)
    {
        NSString* valicode = [NSString stringWithFormat:@"%@",body[@"validCode"]];
        NSString* valicodeseq = [NSString stringWithFormat:@"%@",body[@"validCodeSeq"]];
        AppDelegate* mydelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
        mydelegate.registercode = valicode;
        mydelegate.registerseq = valicodeseq;
        [self.navigationController popViewControllerAnimated:YES];
    }
    //NSLog(@"%@",body[@"validCodeSeq"]);
}

-(bool)isPureInt:(NSString*)input
{
    NSScanner* scann = [NSScanner scannerWithString:input];
    int val;
    bool bRes = [scann scanInt:&val] && [scann isAtEnd];
    
    return  bRes;
}

#pragma -
-(void)showMessageBox:(NSString*)title content:(NSString*)content
{
    UIAlertView* msg = [[UIAlertView alloc] initWithTitle:title message:content delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [msg show];
}

#pragma Textfield delegate
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.phoneNumber.text = @"";
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if([self.phoneNumber isFirstResponder])
    {
        [self.phoneNumber resignFirstResponder];
    }
    
    return  YES;
}
@end
